package com.atguigu.springcloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author JayChan
 * @Description
 * @create 2021-12-02 8:31 PM
 */
@RestController
public class FlowLimitController {

    @GetMapping("/testA")
    public String testA() {
        return "testA";
    }


    @GetMapping("/testB")
    public String testB(){
        return "testB";
    }


    @GetMapping("/testD")
    public String testD(){
//        try {
//            TimeUnit.SECONDS.sleep(1);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println("异常比例testD");
        int age = 10/0;
        return "testD 测试异常比例testD";
    }



    @GetMapping("/testE")
    public String testE(){
        System.out.println("测试异常数");
        int age = 10/0;
        return "testE 测试异常数";
    }



    //对应sentinel的热点规则配置，没有对应不上。

    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
    public String testHostKey(@RequestParam(value = "p1",required = false)String p1,
                              @RequestParam(value = "p2",required = false)String p2){
        int age = 10/0;
        return "testHotKey";
    }


    //这是一个兜底的方法
    public String deal_testHotKey(String p1, String p2, BlockException exception){
        return "------deal_testHotKey";   //sentinel默认提示：Blocked by Sentinel (flow limiting)
    }






}
